package wps.component {
import flash.events.Event;

import mx.binding.utils.BindingUtils;
import mx.binding.utils.ChangeWatcher;
import mx.core.UIComponent;
import mx.events.FlexEvent;

import spark.components.BorderContainer;

import wps.util.GeomUtil;
import wps.util.Register;

public class ElementFocusPane extends BorderContainer {

    //--------------------------------------------------------------------------
    //
    // Constants
    //
    //--------------------------------------------------------------------------

    public static const DEPTH:Number = Number.MAX_VALUE;

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _board:DrawingBoard;

    /**
     * 获取画板
     */
    private function get board():DrawingBoard {
        if (!_board) {
            _board = Register.lookup(DrawingBoard.NAME) as DrawingBoard;
        }
        return _board;
    }

    //----------------------------------
    // target
    //----------------------------------

    private var _target:UIComponent;

    /**
     * 焦点框的目标元素
     */
    public function get target():UIComponent {
        return _target;
    }

    public function set target(target:UIComponent):void {
        _target = target;

        if (target) {
            targetChanged();
            board.addElement(this);
        } else {
            if (board.contains(this)) {
                board.removeElement(this);
            }
        }
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    private function targetChanged():void {
        // 先旋转
        BindingUtils.bindSetter(rotationSetter, target, "elementRotation");
		
		// 再调整到目标的位置大小
		BindingUtils.bindSetter(xSetter, target, "x");
		BindingUtils.bindSetter(ySetter, target, "y");
		BindingUtils.bindSetter(widthSetter, target, "width");
		BindingUtils.bindSetter(heightSetter, target, "height");
    }

    private function xSetter(value:Number):void {
        x = value - getStyle("borderWeight");
    }

    private function ySetter(value:Number):void {
        y = value - getStyle("borderWeight");
    }

    private function widthSetter(value:Number):void {
        width = value + getStyle("borderWeight") * 2;
    }

    private function heightSetter(value:Number):void {
        height = value + getStyle("borderWeight") * 2;
    }

    private function rotationSetter(value:Number):void {
		if (rotation != 0) {
			GeomUtil.rotate(this, -rotation);
		}
		
        if (value != 0) {
            GeomUtil.rotate(this, value);
        }
    }

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function ElementFocusPane() {
        super();

        // 如果在控件在addElement调用之前获取style的值将得到NaN，调用addElement之后才可以获取到正确的值
        // FocusPane在显示前需要计算位置及大小，所以在这里设置borderWeight
        setStyle("borderWeight", 1);
    }

}
}
